package cn.iocoder.yudao.module.els.service.codeGeneratorSerial;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.els.controller.admin.codeGeneratorSerial.vo.*;
import cn.iocoder.yudao.module.els.dal.dataobject.codeGeneratorSerial.CodeGeneratorSerialDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.els.dal.mysql.codeGeneratorSerial.CodeGeneratorSerialMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.els.enums.ErrorCodeConstants.*;

/**
 * 编码生成器序列 Service 实现类
 *
 * @author 吕小文
 */
@Service
@Validated
public class CodeGeneratorSerialServiceImpl implements CodeGeneratorSerialService {

    @Resource
    private CodeGeneratorSerialMapper codeGeneratorSerialMapper;

    @Override
    public Long createCodeGeneratorSerial(CodeGeneratorSerialSaveReqVO createReqVO) {
        // 插入
        CodeGeneratorSerialDO codeGeneratorSerial = BeanUtils.toBean(createReqVO, CodeGeneratorSerialDO.class);
        codeGeneratorSerialMapper.insert(codeGeneratorSerial);
        // 返回
        return codeGeneratorSerial.getId();
    }

    @Override
    public void updateCodeGeneratorSerial(CodeGeneratorSerialSaveReqVO updateReqVO) {
        // 校验存在
        validateCodeGeneratorSerialExists(updateReqVO.getId());
        // 更新
        CodeGeneratorSerialDO updateObj = BeanUtils.toBean(updateReqVO, CodeGeneratorSerialDO.class);
        codeGeneratorSerialMapper.updateById(updateObj);
    }

    @Override
    public void deleteCodeGeneratorSerial(Long id) {
        // 校验存在
        validateCodeGeneratorSerialExists(id);
        // 删除
        codeGeneratorSerialMapper.deleteById(id);
    }

    private void validateCodeGeneratorSerialExists(Long id) {
        if (codeGeneratorSerialMapper.selectById(id) == null) {
            throw exception(CODE_GENERATOR_SERIAL_NOT_EXISTS);
        }
    }

    @Override
    public CodeGeneratorSerialDO getCodeGeneratorSerial(Long id) {
        return codeGeneratorSerialMapper.selectById(id);
    }

    @Override
    public PageResult<CodeGeneratorSerialDO> getCodeGeneratorSerialPage(CodeGeneratorSerialPageReqVO pageReqVO) {
        return codeGeneratorSerialMapper.selectPage(pageReqVO);
    }

}